const { db } = require("../config")
const mongoose = require('mongoose');


//连接数据库[应用数据库]连接配置
let contentUrl = `mongodb://${db.username}:${db.password}@${db.host}:${db.port}/${db.dbname}`

const maindb = mongoose.createConnection(contentUrl);

mongoose.Promise = global.Promise

// 连接成功监听
maindb.on('connected', () => {
    console.log('MongoDB 连接成功');
});

// 连接失败监听
maindb.on('error', (err) => {
    console.log('MongoDB 连接失败', err);
});

// 断开连接监听
maindb.on('disconnected', () => {
    console.log('MongoDB 断开连接');
});

module.exports = {
    maindb
}
